package 递归练习;

import java.util.LinkedList;

public class test2 {//汉诺塔实验
    static LinkedList<Integer> a=new LinkedList<>();
    static LinkedList<Integer> b=new LinkedList<>();
    static LinkedList<Integer> c=new LinkedList<>();
    static void init(int n){
        for (int i = n; i >=1; i--) {
            a.addLast(i);
        }
    }
    public  static  void print(){
        System.out.println("--------------");
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }//利用了递归思想
    static void move(int n,LinkedList<Integer>a,LinkedList<Integer>b,LinkedList<Integer>c){//把所有东西移动到c
        if(n==0){//递归结束条件
            return;
        }
        move(n-1,a,c,b);//借助c把n-1个移动到b
        c.addLast(a.removeLast());//把第n个移动到c
        move(n-1,b,a,c);//借助a把b的n-个移动到c
    }
    public static int element(int i,int j){
        if(j==0||i==j){
            return 1;
        }
        return element(i-1,j-1)+element(i-1,j);
    }
    public static void print2(int n){//杨辉三角
        for (int i = 0; i < n; i++) {
            for(int j=0;j<=i;j++){
                System.out.print(element(i,j));
            }
            System.out.println();
        }
    }
    public static  void printspace(int n,int i){
        int num=(n-1-i)*2;
        for(int j=0;j<num;j++){
            System.out.print(" ");//不允许换行
        }
    }
    public static void print3(int n){
        for(int i=0;i<n;i++){
            printspace(n,i);
            for (int j = 0; j <=i ; j++) {
                System.out.printf("%-4d",element(i,j));
            }
            System.out.println();
        }
    }
    public static int element2(int[][] triangle,int i,int j){//杨辉三角优化版
        if(j==0||i==j){
            return 1;
        }
        return element2(triangle,i-1,j-1)+element2(triangle,i-1,j);
    }
public static void print4(int n){
        int [][] triangle=new int [n][];
    for (int i = 0; i < n; i++) {
        triangle[i]=new int[i+1];

    }
}

    public static void main(String[] args) {
        init(3);
        print();
        move(3,a,b,c);
        print();
        print2(5);
        print3(5);
    }
}
